/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.unipar.oficina.converters;

import br.com.unipar.oficina.modelo.cadastros.Cargo;
import br.com.unipar.oficina.service.cadastros.CargoService;
import br.com.unipar.oficina.service.cadastros.ColaboradorService;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.springframework.web.jsf.FacesContextUtils;

/**
 *
 * @author Thiago Marques
 */
@FacesConverter(value = "converterCargo")
public class ConverterCargo implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value != null) {
            CargoService cargoService = (CargoService) FacesContextUtils.getWebApplicationContext(context).getBean("cargoService");
            return cargoService.getById(Long.valueOf(value));
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value instanceof Cargo) {
            Cargo cargo = (Cargo) value;
            return String.valueOf(cargo.getIdCargo());
        }
        return "";
    }
}
